<?php

namespace backend\modules\personnel\controllers;

use common\models\personnel\Personnel;
use Yii;
use common\models\base\SearchModel;
use common\traits\Curd;
use common\models\personnel\Leave;
use common\enums\StatusEnum;
use backend\controllers\BaseController;

/**
 * 客户端
 *
 * Class ClientController
 * @package backend\modules\member\controllers
 * @author jianyan74 <751393839@qq.com>
 */
class LeaveController extends BaseController
{
    use Curd;

    /**
     * @var Client
     */
    public $modelClass = Leave::class;

    /**
     * 首页
     *
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => ['name','phone'], // 模糊查询
            'defaultOrder' => [
                'starting_time' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);

        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);
        $dataProvider->query
            ->andWhere(['>=', 'status', StatusEnum::DISABLED]);
        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }
    public function actionAjaxEdit()
    {
        $id = Yii::$app->request->get('id');
        $model = $this->findModel($id);

        $this->activeFormValidate($model);
        if ($model->load(Yii::$app->request->post())) {
            return $model->save()
                ? $this->redirect(Yii::$app->request->referrer)
                : $this->message($this->getError($model), $this->redirect(Yii::$app->request->referrer), 'error');
        }
        return $this->renderAjax($this->action->id, [
            'model' => $model,
            'menuDropDownList' => Yii::$app->services->menu->getDropDownts(),
            'menuDropDownLists' => Yii::$app->services->menu->getDropDowns(),
        ]);
    }

    public function actionLeave(){
        $id =Yii::$app->request->get();
        $data =Leave::findOne($id['id']);
        if ($data['report_time']){
            return $this->message("你已经销假了,不能重复销假", $this->redirect(Yii::$app->request->referrer), 'error');
        }
        $data->report_time = date('Y-m-d h:i:s', time());
        if ($data->save()){
            return $this->message("销假成功", $this->redirect(Yii::$app->request->referrer));
        }
    }
}